﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.Routing;
using System.Configuration;
namespace VTNT.Framework.Web.Routing
{
    public class RouteManager
    {
        /*
        Link Ref: http://www.codeproject.com/Articles/37776/Writing-a-complex-custom-configuration-section        
        
        ---------------------------------------------------------------------------
        Step 1: In web.config file
        ---------------------------------------------------------------------------
         
        <configuration>
            <!-- Configuration section-handler declaration area. -->
	        <configSections>
		        <section name="vtntRouting" type="VTNT.Framework.Web.Routing.RoutingSection"/>
            </configSections>

	        <!-- Configuration section settings area. -->
	        <vtntRouting enabled="true">
		        <routes>
			        <add name="defs"	routeUrl="{modname}"			    physicalFile="~/mods/{modname}/default.aspx"/>
			        <add name="mods"	routeUrl="{modname}/{pagename}"	    physicalFile="~/mods/{modname}/{pagename}.aspx"/>
		        </routes>
	        </vtntRouting>
         
            <!-- Configuration other settings area. -->
            .....
        <configuration>         
        
        ---------------------------------------------------------------------------
        Step 2: In Application_Start
        ---------------------------------------------------------------------------
         
        using System.Web.Routing;
        using VTNT.Framework.Web.Routing;         
        
        protected void Application_Start(object sender, EventArgs e)
        {
            RouteManager.RegisterRoutes(RouteTable.Routes);
        }
        
        */

        public static void RegisterRoutes(RouteCollection routes)
        {
            RouteSection vtntRoutes = (RouteSection)ConfigurationManager.GetSection("vtntRouting");

            if (vtntRoutes.Enabled)
            {
                RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");
                routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));

                //Note: Adding route in web.config file
                foreach (RouteElement element in vtntRoutes.Elements)
                {
                    if (element.Name != null && element.Name != "")
                    {
                        try
                        {
                            routes.MapPageRoute(element.Name, element.RouteUrl, element.PhysicalFile);
                        }
                        catch (Exception ex)
                        {
                            string s = ex.Message;
                        }
                    }
                }

                //Note: Adding route directly 
                //routes.MapPageRoute("defs", "{modname}", "~/mods/{modname}/default.aspx");
                //routes.MapPageRoute("mods", "{modname}/{pagename}", "~/mods/{modname}/{pagename}.aspx");
            }
        }
    }
}
